//
//  TreatmentsViewController.m
//  EcmDoctor
//
//  Created by Mac on 16/2/25.
//  Copyright © 2016年 张进. All rights reserved.
//

#import "TreatmentsViewController.h"
#import "TreatmentsTableViewCell.h"
#import "Treatmentsm.h"
#import "UIImageView+AFNetworking.h"
#import "AppUtils.h"
#import "DetailtreatMents.h"
#import "DetailTreatmentsViewController.h"
@interface TreatmentsViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    __weak IBOutlet UITableView *_treatmentListTv;
     NSArray *_askStyAry;
    NSArray *_StyAry;
    NSArray *_info_typeImageAry;
    NSArray *_cure_typeImageAry;
    UILabel *_tishil;
}
@end

@implementation TreatmentsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
     self.view.backgroundColor = [AppUtils getBackground];
    // Do any additional setup after loading the view.
    _treatmentListTv.rowHeight=130;
     _askStyAry=@[@"免费义诊",@"在线问诊",@"电话问诊",@"门诊加号"];
    _StyAry=@[@"自费",@"医保"];
    _info_typeImageAry=@[@"在线",@"在线",@"电话",@"门诊"];
    _cure_typeImageAry=@[@"自费",@"医保"];
     self.navigationItem.titleView=[AppUtils generateNavigationItemTitleViewWithTitle:@"治疗方案"];
    UIImage *tableIcon = [UIImage imageNamed:@"btn_back"];
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithImage:tableIcon style:UIBarButtonItemStylePlain target:self action:@selector(changeToNavRootController:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithPatternImage:tableIcon];
  
    if(self.single)
    {
        [self reloadtableView];
    }
}
-(void)reloadtableView
{
    NSString *docId= [SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY];
    [Treatmentsm  requetDoctorpcWith:docId success:^(NSArray *treatmentsAry) {
        
        _dataAry=[NSArray arrayWithArray:treatmentsAry];
        [_treatmentListTv reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
  
 

}
-(void)changeToNavRootController:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if(!_dataAry.count)
    {
        if(_tishil==nil)
        {
            _tishil=[[UILabel alloc]initWithFrame:CGRectMake(0, 20, kScreenWidth, 30)];
            
            _tishil.textAlignment=NSTextAlignmentCenter;
            _tishil.backgroundColor=[UIColor clearColor];
            
            _tishil.textColor=[UIColor blackColor];
            _tishil.font=[UIFont systemFontOfSize:15];
            
        }
        _tishil.text=@"暂时没有相关治疗方案";
        [tableView addSubview:_tishil];
        
        
    }
    else
    {
        [_tishil removeFromSuperview];
    }
    
    return _dataAry.count;
    

}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell;
    Treatmentsm *tdl=_dataAry[indexPath.row];
    TreatmentsTableViewCell *treatmentslcell=[tableView dequeueReusableCellWithIdentifier:@"treatments" forIndexPath:indexPath];
   [treatmentslcell.iconImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://ecm-user.oss-cn-beijing.aliyuncs.com/image/%@",tdl.user_icon]] placeholderImage:[UIImage imageNamed:@"icon"]];
    treatmentslcell.customerName.text=tdl.user_name;
    treatmentslcell.customerInfo.text=[NSString stringWithFormat:@"%@  %@",tdl.user_sex,tdl.user_age];
    treatmentslcell.customerQd.text=tdl.info_content;
    treatmentslcell.customerDate.text=tdl.indo_date;
    treatmentslcell.selectionStyle=UITableViewCellSelectionStyleNone;
//    for(int i=0;i<_askStyAry.count;i++)
//    {
//        if(i<2)
//        {
//            if([tdl.cure_type isEqualToString:[NSString  stringWithFormat:@"%d",i]])
//            {
//                treatmentslcell.customerSty.image=[UIImage imageNamed:_cure_typeImageAry[i]];
//            }
//        }
//        if([tdl.info_type isEqualToString:[NSString  stringWithFormat:@"%d",i]])
//        {
//            treatmentslcell.customerQt.text=_askStyAry[i];
//            treatmentslcell.customerQtImageView.image=[UIImage imageNamed:_info_typeImageAry[i]];
//            
//        }
//    }
    
    treatmentslcell.customerSty.image=[UIImage imageNamed:_cure_typeImageAry[tdl.cure_type.intValue]];
    treatmentslcell.customerQt.text=_askStyAry[tdl.info_type.intValue];
    treatmentslcell.customerQtImageView.image=[UIImage imageNamed:_info_typeImageAry[tdl.info_type.intValue]];

    cell=treatmentslcell;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   Treatmentsm *tdl=_dataAry[indexPath.row];
    [DetailtreatMents getCureSchemeInfoById:tdl.curescheme_info_id And:tdl.cure_type success:^(NSArray *cureAry) {
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:tdl,@"tdl",cureAry,@"cureAry" ,nil];
      [self performSegueWithIdentifier:@"detailtreatments" sender:dic];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
        
        //需要注意的是self.isViewLoaded是必不可少的，其他方式访问视图会导致它加载 ，在WWDC视频也忽视这一点。
        
        if (self.isViewLoaded && !self.view.window)// 是否是正在使用的视图
        {
            // Add code to preserve data stored in the views that might be
            // needed later.
            
            // Add code to clean up other strong references to the view in
            // the view hierarchy.
            self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
        }
        
    }
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"detailtreatments"]) {
        DetailTreatmentsViewController* detailTreatments = [segue destinationViewController];
        
        NSDictionary *dic=sender;
        
        detailTreatments.dataAry=[NSArray arrayWithArray:dic[@"cureAry"]];
        Treatmentsm *tdl=dic[@"tdl"];
        detailTreatments.user_age=tdl.user_age;
        detailTreatments.user_icon=tdl.user_icon;
        detailTreatments.user_name=tdl.user_name;
        detailTreatments.user_sex=tdl.user_sex;
        detailTreatments.type = tdl.info_type;
        detailTreatments.askId = tdl.ask_info_id;
        detailTreatments.i = _i;
        
    }

}


@end
